package com.iurac.service.impl;

import com.iurac.dao.UserDao;
import com.iurac.domain.User;
import com.iurac.exception.LoginException;
import com.iurac.exception.ManageException;
import com.iurac.service.UserService;
import com.iurac.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    @Transactional(readOnly = true)
    public User login(String account, String password, String role) throws LoginException {
        int count = userDao.getUserCountByAccount(account);
        if(count==0){
            throw new LoginException("该账号不存在");
        }
        User user = userDao.login(account,password,role);
        if(user == null){
            throw new LoginException("密码或角色错误");
        }
        return user;
    }

    @Override
    @Transactional(readOnly = true)
    public String getBorrowCountById(String id) {
        return userDao.getBorrowCountById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<UserVo> getUsersByCondition(String username, String account, String phone, String[] sex, String[] role) {
        return userDao.getUsersByCondition(username,account,phone,sex,role);
    }

    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getDateAboutUser() {
        Map<String, Object> map = new HashMap<>();

        Integer numOfUser = userDao.getNumOfUser();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String today = sdf.format(date);
        Integer numOfBorrowUserToday = userDao.getNumOfBorrowUserToday(today);
        Integer numOfBorrowUser = userDao.getNumOfBorrowUser();
        Integer numOfBorrowUserTotal = userDao.getNumOfBorrowUserTotal();
        map.put("numOfUser",numOfUser);
        map.put("numOfBorrowUserToday",numOfBorrowUserToday);
        map.put("numOfBorrowUser",numOfBorrowUser);
        map.put("numOfBorrowUserTotal",numOfBorrowUserTotal);
        return map;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean deleteUserById(String id) throws ManageException {
        int borrowCount = userDao.getBorrowCountByUid(id);
        if(borrowCount==0){
            throw new ManageException("该用户还有图书未归还");

        }int result = userDao.deleteUserById(id);
        if(result!=1){
            throw new ManageException("删除用户失败");
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean updateUser(User user) throws ManageException {
        int result = userDao.updateUser(user);
        if(result!=1){
            throw new ManageException("更新用户失败");
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean saveUser(User user) throws ManageException {
        int userCount = userDao.getUserCountByAccount(user.getAccount());
        if(userCount!=0){
            throw new ManageException("该账号已经存在");
        }
        int result = userDao.saveUser(user);
        if(result!=1){
            throw new ManageException("添加用户失败");
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean updatePhone(String id, String phone) throws ManageException {
        int result = userDao.updatePhone(id,phone);
        if(result!=1){
            throw new ManageException("更新手机号码失败");
        }
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public User getUserById(String id) {
        return userDao.getUserById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean updatePassword(String id, String password) throws ManageException {
        int result = userDao.updatePassword(id,password);
        if(result!=1){
            throw new ManageException("更新密码失败");
        }
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public UserVo getUserByAccount(String account) {
        return userDao.getUserByAccount(account);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean register(User user) throws ManageException {
        return saveUser(user);
    }
}
